﻿using APP.QUANLY.Common;
using MetroUI.Forms;
using RMAS.DAL;
using Sys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMAS
{
    public partial class frmDM_Branch : MetroForm
    {
        DM_BRANCH_Provider _prov = new DM_BRANCH_Provider();
        RegistrySetting setting = new RegistrySetting();
        public DateTime DateBegin;
        public DateTime DateEnd;
        public bool HasLicenceKey;
        public string Version;
        public Decimal Branch_Revenue;
        private string SerialKey = "";
        private bool RunFirstTime = false;

        public frmDM_Branch()
        {
            InitializeComponent();
        }
        public frmDM_Branch(string SerialKey, bool RunFirstTime)
        {
            InitializeComponent();
            this.SerialKey = SerialKey;
            this.RunFirstTime = RunFirstTime;
        }
        private void frmDM_Branch_Load(object sender, EventArgs e)
        {
            getDM_BRANCH();
        }
        private void getDM_BRANCH()
        {
            DM_BRANCH obj = null;
            try
            {
                DM_BRANCH_Provider _prov = new DM_BRANCH_Provider();
                string CPUID = Utils.GetCPUID();
                DM_BRANCHList list = _prov.GetAll();
                if (list != null && list.Count > 0)
                {
                    obj = list.Find(CPUID);
                    if (obj != null)
                    {
                        StaticVar.CPUID = obj.ID;
                        StaticVar.CurrBranch = obj;
                    }
                }
            }
            catch { }
            LoadValuesToUI(obj);
        }
        private void LoadValuesToUI(DM_BRANCH obj)
        {
            if (obj == null)
            {
                obj = new DM_BRANCH();
                obj.ID = Utils.GetCPUID();

                //string DayBegin = "", MonthBegin = "", YearBegin = "";
                //setting.SetRegValueLicence30RunFirst(out SerialKey, out DayBegin, out MonthBegin, out YearBegin);
                SoftLicence SL = setting.RunFirst();
                if (SL == null)
                    SL = setting.RunContinue();
                //DateTime dt = new DateTime(Int32.Parse(YearBegin), Int32.Parse(MonthBegin), Int32.Parse(DayBegin));
                DateTime dt = SL.DtBegin;
                obj.DateTimeBeginLicence = dt;
                obj.DateTimeEndLicence = dt.AddDays(30);
                obj.IsUse = true;
            }

            labMa.Text = obj.ID;
            labIDPos.Text = obj.IDPOS.ToString();
            txtTen.Text = obj.Branch_Name;
            txtDiaChi.Text = obj.Branch_Address;
            txtDienThoai.Text = obj.Branch_Phone;
            txtEmail.Text = obj.Branch_Email;
            txtFax.Text = obj.Branch_Fax;
            txtQuanLy.Text = obj.Branch_Director;
            txtFacebook.Text = obj.Branch_Facebook;
            txtGhiChu.Text = obj.Branch_Note;
            labBranch_Revenue.Text = obj.Branch_Revenue.ToString();
            labDateTimeBeginLicence.Text = obj.DateTimeBeginLicence.ToString();
            labDateTimeEndLicence.Text = obj.DateTimeEndLicence.ToString();
            labHasLicenceKey.Text = obj.HasLicenceKey.ToString();
            labVersion.Text = obj.Version;
            labIsUse.Text = obj.IsUse.ToString();
            pbtLogo.Image = ImageUtil.GetImageFromBytes(obj.Branch_Logo);
            ptbHinhCongTy.Image = ImageUtil.GetImageFromBytes(obj.Branch_Image);

            Branch_Revenue = obj.Branch_Revenue;
            DateBegin = obj.DateTimeBeginLicence;
            DateEnd = obj.DateTimeEndLicence;
            Version = obj.Version;
            HasLicenceKey = obj.HasLicenceKey;
        }
        private DM_BRANCH loadUIToValues()
        {
            DM_BRANCH obj = new DM_BRANCH();
            try
            {
                if (labMa.Text != "" && txtTen.Text != "" && txtQuanLy.Text != "" && txtDiaChi.Text != "" && txtDienThoai.Text != "" && txtEmail.Text != "" && txtFax.Text != "")
                {
                    byte[] logo = ImageUtil.GetBytesFromImage(pbtLogo.Image);
                    byte[] coty = ImageUtil.GetBytesFromImage(ptbHinhCongTy.Image);
                    obj.Fill(labMa.Text, int.Parse(labIDPos.Text), txtTen.Text, txtDiaChi.Text, txtDienThoai.Text, txtFax.Text, txtEmail.Text, txtGhiChu.Text, logo, coty, txtQuanLy.Text, decimal.Parse(labBranch_Revenue.Text), txtFacebook.Text, DateTime.Parse(labDateTimeBeginLicence.Text), DateTime.Parse(labDateTimeEndLicence.Text), bool.Parse(labHasLicenceKey.Text), labVersion.Text, bool.Parse(labIsUse.Text));
                }
                else
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin!");
                    obj = null;
                }
            }
            catch
            {
            }
            return obj;
        }

        private bool XuLy()
        {
            bool KQ = false;
            DM_BRANCH obj = loadUIToValues();
            if (obj != null)
            {
                DM_BRANCH obj1 = _prov.GetBy_ID(obj.ID);
                if (obj1 == null)
                {
                    KQ = _prov.Cmd_ThemDM_BRANCH(obj);
                }
                else
                {
                    KQ = _prov.Cmd_CapNhatDM_BRANCH(obj);
                }
                MessageBox.Show(_prov.ErrMsg.Replace("DM_BRANCH", "thông tin công ty"));
                if (KQ) 
                    this.DialogResult = DialogResult.OK;   
            }
            return KQ;
        }
        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            try
            {
                bool KQ = false;
                //string Trial = "";
                KQ = XuLy();
                if (KQ)
                {
                    base.Hide();
                    SoftLicence SL = new SoftLicence();
                    RunTypes type = setting.CheckDemo(SerialKey, out SL);
                    switch (type)
                    {
                        case RunTypes.Expired:
                            frmRegisterSoftWare fregister1 = new frmRegisterSoftWare(SL, type);
                            if (fregister1.ShowDialog() == DialogResult.OK)
                            {
                                base.DialogResult = DialogResult.OK;
                            }
                            break;
                            //frmHeplerInfo fhelper = new frmHeplerInfo();
                            //fhelper.ShowDialog();
                            //break;
                        case RunTypes.Full:
                            base.DialogResult = DialogResult.OK;
                            break;
                        case RunTypes.Trial:
                            frmRegisterSoftWare fregister = new frmRegisterSoftWare(SL, type);
                            if (fregister.ShowDialog() == DialogResult.OK)
                            {
                                base.DialogResult = DialogResult.OK;
                            }
                            break;
                        case RunTypes.UnKnown:
                           MessageBox.Show("Vui lòng liên hệ nhà cung cấp theo thông tin sau!!!");
                            frmHeplerInfo fhelper1 = new frmHeplerInfo();
                            fhelper1.ShowDialog();
                            Environment.Exit(0);
                            break;
                    }
                }
                else
                {
                    base.Close();
                }
            }
            catch { }
        }

        private void btnChonLogo_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image Files (*.png *.jpg *.bmp) |*.png; *.jpg; *.bmp|All Files(*.*) |*.*";
            dialog.InitialDirectory = @"C:\";
            dialog.Title = "Vui lòng chọn một hình để làm logo.";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                pbtLogo.Image = Image.FromFile(dialog.FileName);
            }
            dialog.Dispose();
        }
        private void btnXoaLogo_Click(object sender, EventArgs e)
        {
            pbtLogo.Image = null;
        }
        private void btnXoaHinhCoTy_Click(object sender, EventArgs e)
        {
            ptbHinhCongTy.Image = null;
        }
        private void btnChonHinhCoTy_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image Files (*.png *.jpg *.bmp) |*.png; *.jpg; *.bmp|All Files(*.*) |*.*";
            dialog.InitialDirectory = @"C:\";
            dialog.Title = "Vui lòng chọn một hình đại điện cho công ty.";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                ptbHinhCongTy.Image = Image.FromFile(dialog.FileName);
            }
            dialog.Dispose();
        }
      
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Để sử dụng chương trình. Bạn phải đăng ký thông tin công ty!\n Bạn có muốn thoát hệ thống không?", "Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
                Application.Exit();
        }

        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox txt = sender as TextBox;
                if (txt.Name == txtTen.Name)
                    UIHelper.txt_KeyPress(sender, txtQuanLy, e);
                if (txt.Name == txtQuanLy.Name)
                    UIHelper.txt_KeyPress(sender, txtDiaChi, e);
                if (txt.Name == txtDiaChi.Name)
                    UIHelper.txt_KeyPress(sender, txtDienThoai, e);
                if (txt.Name == txtDienThoai.Name)
                    UIHelper.txt_KeyPress(sender, txtFax, e);
                if (txt.Name == txtFax.Name)
                    UIHelper.txt_KeyPress(sender, txtEmail, e);
                if (txt.Name == txtEmail.Name)
                    UIHelper.txt_KeyPress(sender, txtFacebook, e);
                if (txt.Name == txtFacebook.Name)
                    UIHelper.txt_KeyPress(sender, txtGhiChu, e);
                if (txt.Name == txtGhiChu.Name)
                    UIHelper.txt_KeyPress(sender, btnDangKy, e);
            }
            catch { }
        }
    }
}
